package com.aysafety.iotdataplatformserver.listener;

import com.alibaba.fastjson.JSON;
import com.aysafety.core.DBDriver.hbase.HBaseDriver;
import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.WindsModel;
import com.aysafety.iotdataplatformserver.dao.WindsDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

// 风速
@Component public class WindsListener {

    private final String type = "Winds";

    private final String tableName = "WindsRealTime";

    private final String[] structure = new String[] {"Winds"};
    @Autowired KsTreeDataListener ksTreeDataListener;
    @Autowired WindsDao windsDao;
    private Logger logger = LoggerFactory.getLogger(ErrorListener.class);

    public boolean createTable() throws IOException {
        return HBaseDriver.createTable(this.tableName, this.structure);
    }

    public void start(Map<String, Object> result) {
        try {
            String clientId = result.get("clientId").toString();
            if (!result.get("dataType").toString().equals("histroy")) {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    WindsModel winds = this.transformData(listData);
                    this.save(winds, clientId);
                }
            } else {
                List<Map<String, Object>> listDatas = JSON.parseObject(result.get("data").toString(), List.class);
                for (Map<String, Object> listData : listDatas) {
                    WindsModel winds = this.transformData(listData);
                    this.save(winds, clientId);
                }
            }
        } catch (IOException e) {
            logger.info("WindsListener:", e);
        }
    }

    public boolean save(WindsModel winds, String clientId) throws IOException {
        String timestamp = String.valueOf(winds.getDatetime().getTime());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("tableId", winds.getEntId() + "." + "WindsRealTime");
        map.put("pk", "\'" + timestamp+".:." + winds.getDeviceId()+ "\'");
        map.put("clientId", "\'" + clientId + "\'");
        map.put("deviceId", "\'" + winds.getDeviceId() + "\'");
        map.put("entId", "\'" + winds.getEntId() + "\'");
        map.put("unit", "\'" + winds.getUnit() + "\'");
        map.put("workingFace", "\'" + winds.getWorkingFace() + "\'");
        map.put("datetime", winds.getDatetime().getTime());
        map.put("monitoringValue", winds.getMonitoringValue());
        map.put("poklad", winds.getPoklad());
        map.put("smallestRange", winds.getSmallestRange());
        map.put("maximumRange", winds.getMaximumRange());
        map.put("stationName", "\'" + winds.getStationName() + "\'");
        map.put("status", winds.isStatus());
        map.put("powerCutArea", "\'" + winds.getPowerCutArea() + "\'");
        map.put("powerReconnectionLimit", winds.getPowerReconnectionLimit());
        map.put("powerCutLimit", winds.getPowerCutLimit());
        logger.info("map=" + map);
        windsDao.insertWindsRealTime(map);
        ksTreeDataListener.generateTreeData(winds.getEntId(), this.type, winds.getWorkingFace(), winds.getDeviceId(),
            winds.getDatetime(), "");
        return true;
    }

    public WindsModel transformData(Map<String, Object> result) {
        WindsModel winds = new WindsModel();
        if (null != result.get("entId")) {
            winds.setEntId(result.get("entId").toString());
        } else {
            winds.setEntId("无数据");
        }
        if (null != result.get("deviceId")) {
            winds.setDeviceId(result.get("deviceId").toString());
        } else {
            winds.setDeviceId("无数据");
        }
        winds.setMonitoringValue(Double.valueOf(result.get("monitoringValue").toString()));
        if (null != result.get("poklad")) {
            winds.setPoklad(Double.valueOf(result.get("poklad").toString()));
        } else {
            winds.setPoklad(-1.0);
        }
        winds.setDatetime(DateUtils.fomatDateTime(Long.valueOf(result.get("datetime").toString())));
        if (null != result.get("workingFace")) {
            winds.setWorkingFace(result.get("workingFace").toString());
        } else {
            winds.setWorkingFace("无数据");
        }
        if (null != result.get("unit")) {
            winds.setUnit(result.get("unit").toString());
        } else {
            winds.setUnit("无数据");
        }
        if (null != result.get("smallestRange")) {
            winds.setSmallestRange(Double.valueOf(result.get("smallestRange").toString()));
        } else {
            winds.setSmallestRange(-1.0);
        }
        if (null != result.get("maximumRange")) {
            winds.setMaximumRange(Double.valueOf(result.get("maximumRange").toString()));
        } else {
            winds.setSmallestRange(-1.0);
        }
        if (null != result.get("stationName")) {
            winds.setStationName(result.get("stationName").toString());
        } else {
            winds.setUnit("无数据");
        }
        if (null != result.get("status")) {
            if (result.get("status").toString().equals("1")) {
                winds.setStatus(TRUE);
            } else {
                winds.setStatus(FALSE);
            }
        } else {
            winds.setStatus(FALSE);
        }
        if (null != result.get("powerCutArea")) {
            winds.setPowerCutArea(result.get("powerCutArea").toString());
        } else {
            winds.setPowerCutArea("无数据");
        }
        if (null != result.get("powerReconnectionLimit")) {
            winds.setPowerReconnectionLimit(Double.valueOf(result.get("powerReconnectionLimit").toString()));
        } else {
            winds.setPowerReconnectionLimit(-1.0);
        }
        if (null != result.get("powerCutLimit")) {
            winds.setPowerCutLimit(Double.valueOf(result.get("powerCutLimit").toString()));
        } else {
            winds.setPowerCutLimit(-1.0);
        }
        return winds;
    }
}
